package com.cruise.game.controller.common;

import com.cruise.game.common.api.ApiResult;
import com.cruise.game.common.utils.JwtUtils;
import com.cruise.game.controller.BaseController;
import com.cruise.game.model.param.common.SetAdminParam;
import com.cruise.game.model.param.common.WxLoginParam;
import com.cruise.game.model.vo.common.LoginResultVO;
import com.cruise.game.model.vo.common.TokenVO;
import com.cruise.game.service.common.AuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 认证控制器
 *
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Api(tags = "认证相关接口")
@RestController
@RequestMapping("/api/auth")
public class AuthController extends BaseController {

    @Resource
    private AuthService authService;

    @Resource
    private JwtUtils jwtUtils;

    /**
     * 微信登录
     */
    @ApiOperation("微信登录")
    @PostMapping("/wx-login")
    public ApiResult<LoginResultVO> wxLogin(@Validated @RequestBody WxLoginParam param) {
        log.info("微信登录请求: {}", param.getCode());
        return ApiResult.success("登录成功", authService.wxLogin(param));
    }

    /**
     * 刷新token
     */
    @ApiOperation("刷新token")
    @PostMapping("/refresh-token")
    public ApiResult<TokenVO> refreshToken(HttpServletRequest request) {
        String token = jwtUtils.getTokenFromRequest(request);
        return ApiResult.success("Token已刷新", authService.refreshToken(token));
    }

    /**
     * 设置为管理员
     */
    @ApiOperation("设置为管理员")
    @PostMapping("/set-admin")
    public ApiResult<LoginResultVO> setAdmin(@Validated @RequestBody SetAdminParam param) {
        return ApiResult.success("设置管理员成功", authService.setAdmin(param, getCurrentUserId()));
    }
}